import { reqFirstRequirement, 
  reqUpdateFirstRequirement, 
  reqAddFirstRequirement,
reqDelFirstRequirement } from './server';
import { message } from 'antd';

export const models = {
  name: 'graduationNeed',
  model: {
    state: {
      firstRequirement: [],
      loading: false,
    },
    reducers: {
      setFirstRequirement(state, payload) {
        return { ...state, firstRequirement: payload }
      },
      setLoading(state, payload) {
        return { ...state, loading: payload }
      }
    },
    effects: dispatch => {
      return {
        async getFirstRequirement() {
          dispatch.graduationNeed.setLoading(true);
          try {
            const { data, code, msg } = await reqFirstRequirement();
            if(code == 0){
              data.sort((a,b) => {
                return a.firstRequirementNo-b.firstRequirementNo
              })
              await dispatch.graduationNeed.setFirstRequirement(data);
            }
            code !== 0 && await message.error(msg);
          } finally {
            dispatch.graduationNeed.setLoading(false);
          }
        },
        async updateRequirement(params) {
          const { code, msg } = await reqUpdateFirstRequirement(params);
          code == 0 && await dispatch.graduationNeed.getFirstRequirement();
          code !== 0 ? message.error(msg) : message.success('更新成功');
        },
        async addRequirement(params) {
          const { code, data, msg } = await reqAddFirstRequirement(params);
          
          code == 0 && await dispatch.graduationNeed.getFirstRequirement();
          code !== 0 ? await message.error('插入失败') : await message.success('插入成功');
        },
        async delRequirement(params){
          const { code, data, msg } = await reqDelFirstRequirement(params);
          
          code == 0 && await dispatch.graduationNeed.getFirstRequirement();
          code !== 0 ? await message.error('删除失败') : await message.success('删除成功');
        }
      }
    }
  }

}